﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Pre_Purchase_Request : System.Web.UI.Page
    {
        IBLLTree bllTree = new TreeBLL();
        IBLLSysYearType bllYearType = new SysYearTypeBLL();
        IBLLMasterPurchaseType bllPurchaseType = new MasterPurchaseTypeBLL();
        IBLLSysRecordStatus bllStatus = new SysRecordStatusBLL();
        IBLLSysProgress bllProgress = new SysProgressBLL();
        IBLLPrePurchaseRequest bllPPR = new PrePurchaseRequestBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        SysControlBLL bllSysCon = new SysControlBLL();
        SysUseMethodBLL bllUseMethod = new SysUseMethodBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
                LoadYearType();
                LoadPurchaseType();
                LoadRecordStatus();
                LoadJobSiteTree();
                LoadUserInfo();
                LoadProgress();
                LoadDataGrid();
            }
        }

        protected void LoadDefault()
        {
            SysControlInfo infoDefault = bllSysCon.GetDefaultData();
            if (infoDefault.YearType != null)
            {
                cbYearType.SelectedValue = infoDefault.YearType.ToString();
                Session["yearSeq"] = cbYearType.SelectedValue;
                Session["yearSeq_Urgent"] = cbYearType.SelectedValue;
                Session["InvJobSite"] = infoDefault.InvJobSite.ToString();
                
            }
            if (infoDefault.Year != null)
            {
                txtYear.Text = infoDefault.Year.ToString();
                Session["year"] = txtYear.Text;
                Session["year_Urgent"] = txtYear.Text;
            }
        }
        protected void LoadUserInfo()
        {
            HrmEmployeeInfo info = bllEmployee.GetHrmEmployee(Int32.Parse(Session["EmpSeq"].ToString()));

            cbJobSite.SelectedValue = info.WorkingJobSiteSeq.ToString();
            cbJobSite.Enabled = cbJobSite.SelectedValue == Session["InvJobSite"].ToString();
            Session["JobSiteSeq"] = info.WorkingJobSiteSeq.ToString();
                
        }
        protected void LoadDataGrid()
        {
            int YearTypeSeq = Convert.ToInt32(cbYearType.SelectedValue.ToString());
            int Year = Convert.ToInt32(txtYear.Text);
            Nullable<int> JobSiteSeq = null;

            if (Session["JobSiteSeq"].ToString() != Session["InvJobSite"].ToString())
                JobSiteSeq = Int32.Parse(Session["JobSiteSeq"].ToString());

            //Nullable<int> JobSiteSeq = (cbJobSite.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbJobSite.SelectedValue.ToString()) : null;
            Nullable<int> PcType = (cbPurchaseType.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbPurchaseType.SelectedValue.ToString()) : null;
            Nullable<int> ProgressSeq = (cbProgress.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbProgress.SelectedValue.ToString()) : null;
            string StatusSeq = (cbStatus.SelectedIndex >= 0) ? cbProgress.SelectedValue.ToString() : "";

            IList<PrePurchaseRequestInfo> infoList = bllPPR.GetPrePurchaseRequest(YearTypeSeq, Year, JobSiteSeq, PcType, ProgressSeq, StatusSeq);
            Grid.DataSource = infoList;
            GridGroupByExpression g;
            Grid.MasterTableView.GroupByExpressions.Clear();
            g = new GridGroupByExpression(Grid.Columns[1]);
            Grid.MasterTableView.GroupByExpressions.Add(g);
            Grid.DataBind();
        }

        protected void LoadDataGrid_Urgent()
        {
            int YearTypeSeq = Convert.ToInt32(cbYearType.SelectedValue.ToString());
            int Year = Convert.ToInt32(txtYear.Text);

            //Nullable<int> JobSiteSeq = (cbJobSite.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbJobSite.ItemTemplate.FindControl() : null;

            Nullable<int> JobSiteSeq = (cbJobSite.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbJobSite.SelectedValue.ToString()) : null;
            Nullable<int> ProgressSeq = (cbProgress.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbProgress.SelectedValue.ToString()) : null;
            string StatusSeq = (cbStatus.SelectedIndex >= 0) ? cbProgress.SelectedValue.ToString() : "";

            IList<PrePurchaseRequestInfo> infoList = bllPPR.GetPrePurchaseRequestUrgent(YearTypeSeq, Year, JobSiteSeq, ProgressSeq, StatusSeq);
            Grid_Urgent.DataSource = infoList;
            GridGroupByExpression g;
            Grid_Urgent.MasterTableView.GroupByExpressions.Clear();
            g = new GridGroupByExpression(Grid_Urgent.Columns[1]);
            Grid_Urgent.MasterTableView.GroupByExpressions.Add(g);
            Grid_Urgent.DataBind();
        }

        protected void LoadYearType()
        {
            IList<SysYearTypeInfo> infoList = bllYearType.GetSysYearType();
            cbYearType.DataSource = infoList;
            cbYearType.DataBind();
        }

        protected void LoadPurchaseType()
        {
            IList<MasterPurchaseTypeInfo> infoList = bllPurchaseType.GetMasterPurchaseType();
            cbPurchaseType.DataSource = infoList;
            cbPurchaseType.DataBind();
            cbPurchaseTypeNormal.DataSource = infoList;
            cbPurchaseTypeNormal.DataBind();
            cbPurchaseTypeUrgent.DataSource = infoList;
            cbPurchaseTypeUrgent.DataBind();
        }

        protected void LoadRecordStatus()
        {
            IList<SysRecordStatusInfo> infoList = bllStatus.GetSysRecordStatusTransaction();
            cbStatus.DataSource = infoList;
            cbStatus.DataBind();
        }

        public void LoadJobSiteTree()
        {
            IList<TreeInfo> info = bllTree.GetJobSiteTree();
            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");

            tree.DataSource = info;
            tree.DataBind();
        }

        protected void LoadProgress()
        {
            IList<SysProgressInfo> infoList = bllProgress.GetSysProgress();
            cbProgress.DataSource = infoList;
            cbProgress.DataBind();
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            IList<PrePurchaseRequestInfo> infoList = new List<PrePurchaseRequestInfo>();
            foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
            {
                PrePurchaseRequestInfo info = new PrePurchaseRequestInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllPPR.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }
            LoadDataGrid();
        }

        protected void Delete_Urgent_Click(object sender, EventArgs e)
        {
            IList<PrePurchaseRequestInfo> infoList = new List<PrePurchaseRequestInfo>();
            foreach (GridDataItem data in Grid_Urgent.MasterTableView.GetSelectedItems())
            {
                PrePurchaseRequestInfo info = new PrePurchaseRequestInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllPPR.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }
            LoadDataGrid_Urgent();
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "Normal")
            {
                LoadDataGrid();
                Session["yearSeq"] = cbYearType.SelectedValue;
                Session["year"] = txtYear.Text;
            }
            else if (RadTabStrip1.SelectedTab.Value == "Urgent")
            {
                LoadDataGrid_Urgent();
                Session["yearSeq_Urgent"] = cbYearType.SelectedValue;
                Session["year_Urgent"] = txtYear.Text;
            }
        }

        protected void RadManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "CloseWindow")
            {
                LoadDataGrid();
            }
            else
            {
                LoadDataGrid_Urgent();
            }
        }

        protected void Grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_Urgent_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            LoadDataGrid_Urgent();
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "Normal")
            {
                LoadDataGrid();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Urgent")
            {
                LoadDataGrid_Urgent();
            }

            panelSearchForNormal.Visible = (RadTabStrip1.SelectedTab.Value == "Normal");
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid();
        }

        protected void clearFilter_Urgent_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid_Urgent.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid_Urgent.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid_Urgent();
        }

        protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                //double price = 0;
                //GridDataItem dataBoundItem = e.Item as GridDataItem;

                //if (((Label)(dataBoundItem["NetAmount"].FindControl("lblNetAmount"))).Text.Length > 0)
                //{
                //    price = double.Parse(((Label)(dataBoundItem["NetAmount"].FindControl("lblNetAmount"))).Text);
                //}

                //totalPrice += price;
            }
        }
    }
}
